package com.zby.big_event_zby.utils;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import java.util.Map;

/**
 * @version 1.0
 * @auther ZBY
 */
//自定义拦截器交给springioc容器管理
@Component
public class Interceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception{
        //令牌验证
        String token = request.getHeader("Authorization");

        if(token == null || token.equals("")){
            response.setStatus(401);
            return false;
        }
        Map<String, Object> stringObjectMap = JwtUtils.pareseToken(token);
        ThreadLocalUtils.set(stringObjectMap);
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        //清除线程
        ThreadLocalUtils.remove();
    }
}
